#!/usr/bin/env python
import roslib; roslib.load_manifest("speech_recognition")
import rospy
from std_msgs.msg import String

from config import *
import subprocess

sep1 = "================================================================================"
sep2 = "--------------------------------------------------------------------------------"
valid_modes = ["", "learning", "commands"]
print_lines = [sep1,
               "This program lets you control the speech recognition system.",
               "Press enter to start recording speech or type a mode name to switch modes.",
               "Valid modes are: " + ", ".join(valid_modes[1:]),
               sep1]

def getinput():
    rospy.init_node(INPUT_NODE_NAME)
    pub = rospy.Publisher(ASR_CHANGEMODE_TOPIC, String)
    rospy.Subscriber(ASR_MSGS_TOPIC, String, change_msg)
    while not rospy.is_shutdown():
        subprocess.call(['clear'])
        for line in print_lines:
            print line
        message = raw_input()
        message = message.strip().lower()
        if message in valid_modes:
            if message == "":
                message = "start"
            pub.publish(String(message))
        else:
            print "Invalid mode. Please re-enter your choice."        

def change_msg(string):
    print string.data
    print sep2
    
if __name__ == '__main__':
    try:
        getinput()
    except rospy.ROSInterruptException: pass
